<?php

class IndexController extends Zend_Controller_Action
{
    protected $_config = null;

    public function init()
    {
        /* Initialize action controller here */
		$bootstrap = $this->getInvokeArg('bootstrap');
        if (null == $this->_config) {
            $this->_config = $bootstrap->getResource('config');
        }
    }

    public function indexAction()
    {
        // action body
        $cid = $this->_request->getParam('cid', 1);
		$page = $this->_request->getParam('page', 1);

		$videoDb = new Application_Model_DbTable_Videos(); 
		$select = $videoDb->select();
		$select->where('tid = ?', 9)
		       ->where('left(`datetime`, 10) = \''. date('Y-m-d').'\'')
			   ->order('datetime desc');

		$paginatorAdapter = new Zend_Paginator_Adapter_DbSelect(clone $select);
		$select->from($videoDb->info(Zend_Db_Table_Abstract::NAME), array('count' => 'count(*)'));
		$count = $videoDb->fetchRow($select)->count;
		$paginatorAdapter->setRowCount((int)$count);
		$paginator = new Zend_Paginator($paginatorAdapter);
		$paginator->setItemCountPerPage(20);
		$paginator->setCurrentPageNumber($page);
		$videos = $paginator->getItemsByPage($page);

		$this->view->paginator = $paginator;
		$this->view->page = $page;
		$this->view->pagecount = ceil($count / 20);
		$this->view->recordcount = $count;
		$this->view->videos = $videos;

		$categoryDb = new Application_Model_DbTable_Category();
		$select = $categoryDb->select()->order('id');
		$cates = $categoryDb->fetchAll($select)->toArray();
		$this->view->cates = $cates;
    }
}
